package com.lry.jvm.instructions.base;

import com.lry.jvm.rtda.Frame;
/**
 * 跳转指令
 * BranchInstruction表示跳转指令，Offset字段存放跳转偏移量。
 * FetchOperands（）方法从字节码中读取一个uint16整数，转成int后赋 给Offset字段
 */
public abstract class BranchInstruction  implements Instruction{

    protected int offset;

    @Override
    public void fetchOperands(BytecodeReader reader) throws Exception {
        this.offset = reader.read16();
    }

    @Override
    public abstract void execute(Frame frame) throws Exception;

    public void branch(Frame frame,int offset){
        int pc = frame.getThread().getPc();
        frame.setNextPc(pc + offset);
    }
}
